package son.hut.vn.app.ui;

import son.hut.vn.app.R;
import son.hut.vn.app.infor.WebserviceConfig;
import son.hut.vn.app.utility.SmartDialogUtility;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class BaseActivity extends Activity{
	
	protected ProgressDialog progressDialog;
	
    /**
     * Check invalid token
     */
    public void checkInvalidToken(String message)
    {
        if (message.equalsIgnoreCase(WebserviceConfig.STRING_INVALID_TOKEN)
            || message.equalsIgnoreCase(WebserviceConfig.STRING_EXPIRE_TOKEN))
        {
            // Logout when invalid token
            logoutInvalidExpireToken();
        }
        else
        {
            showCustomPositiveDialog(R.string.dialog_title_error,
                R.drawable.icon_error, message, R.drawable.button_ok);
        }
    }

    /**
     * Logout because of invalid token
     */
    protected void logoutInvalidExpireToken()
    {

        logoutNormally();
    }
	
    protected void logoutNormally() {
		
	}
    /**
     * Show custom dialog with only positive button
     * 
     * @param titleId
     * @param iconId
     * @param messageId
     * @param positiveBtnId
     */
    public void showCustomPositiveDialog(int titleId, int iconId,
        int messageId, int positiveBtnId)
    {
        SmartDialogUtility dialog = new SmartDialogUtility(this, titleId, iconId,
            messageId, positiveBtnId);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = LayoutParams.WRAP_CONTENT;
        lp.height = LayoutParams.WRAP_CONTENT;
        dialog.show();
        dialog.getWindow().setAttributes(lp);
    }

    /**
     * @param titleId
     * @param iconId
     * @param message
     * @param positiveBtnId
     */
    public void showCustomPositiveDialog(int titleId, int iconId,
        String message, int positiveBtnId)
    {
    	SmartDialogUtility dialog = new SmartDialogUtility(this, titleId, iconId, message,
            positiveBtnId);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = LayoutParams.WRAP_CONTENT;
        lp.height = LayoutParams.WRAP_CONTENT;
        dialog.show();
        dialog.getWindow().setAttributes(lp);
    }
    
    /**
     * Open progress dialog
     * 
     * @param text
     */
    public void showProgressDialog(String text)
    {
        if (progressDialog == null)
        {
            progressDialog = ProgressDialog.show(BaseActivity.this,
                null, text, true);
            progressDialog.setCancelable(false);
        }
    }

    /**
     * Open progress dialog
     */
    public void showProgressDialog()
    {
        showProgressDialog(getString(R.string.message_please_wait));
    }

    /**
     * Close progress dialog
     */
    public void closeProgressDialog()
    {
        if (progressDialog != null)
        {
            progressDialog.cancel();
            progressDialog = null;
        }
    }
    
    // ======================= KEYBOARD MANAGER =======================

    /**
     * Show soft keyboard
     * 
     * @param editText
     */
    public void showKeyboard(EditText editText)
    {
        InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null)
        {
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                0);
        }
    }

    /**
     * Hide soft keyboard
     * 
     * @param editText
     */
    public void hideKeyboard(EditText editText)
    {
        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
            editText.getWindowToken(), 0);
    }

}
